package cn.mesmile.amqp.controller;

import cn.hutool.json.JSONUtil;
import cn.mesmile.amqp.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author zb
 * @date 2021/3/17 23:39
 * @Description:
 */
@RequestMapping("/api/v1/test")
@RestController
public class MerchantController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {


        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            System.out.println("---------------交换机到队列---------------------------");
            System.out.println("message = " + message);
            System.out.println("replyText = " + replyText);
            System.out.println("exchange = " + exchange);
            System.out.println("routingKey = " + routingKey);
            System.out.println("------------------交换机到队列------------------------");

        });

        // 改变状态 TODO
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            // 回调
            System.out.println(">>>>>>>>>>>> correlationData.toString() = " + correlationData);
            System.out.println(">>>>>>>>>>>> ack = " + ack);
            System.out.println(">>>>>>>>>>>> cause = " + cause);

            if (ack) {
                // TODO 消息发送成功
                System.out.println(">>>>> 【生产者到交换机】交换机接收到消息");
            } else {
                // todo 记录消息发送失败
                System.out.println(">>>>> 消息发送失败");
            }
        });

    }


    @GetMapping("/register")
    public Map<String, Object> register() {

        rabbitTemplate.convertAndSend(RabbitMQConfig.NEW_MERCHANT_EXCHANGE, "new.merchant.test.test2", ">>>>>>>>>> 账号通过审核");

        Map<String, Object> map = new HashMap<>();
        map.put("message", " 注册成功");
        map.put("point", " 请在10秒内上传信息");

        return map;
    }


    @GetMapping("/order")
    public Map<String,Object> order(){

        // 发送消息
        for (int i = 0; i < 3 ; i++) {
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"order.log.test","创建一个订单 " + i);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("message", " 订单成交成功");
        map.put("point", " 请在10秒内上传信息");

        return map;
    }

    @GetMapping("/send")
    public Map<String,Object> send(){
        //创建消费对象，并指定全局唯一ID(这里使用UUID，也可以根据业务规则生成，只要保证全局唯一即可)
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("text/plain");
        messageProperties.setContentEncoding("UTF-8");


        // 发送消息
        for (int i = 0; i < 3 ; i++) {
            messageProperties.setMessageId(UUID.randomUUID().toString());
            Message message = new Message("hello,message idempotent!".getBytes(), messageProperties);
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TEST,"test.send",message);
//            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TEST,"test.send","创建一个订单 " + i);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("message", " 订单成交成功");
        map.put("point", " 请在10秒内上传信息");

        return map;
    }

}
